package com.test.mess;


import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.concurrent.*;
import java.util.stream.IntStream;
import java.time.Duration;


public class JavaDownloadFileFromURLWithVT {

    private static LocalDate now = LocalDate.now();
    private static String date = now.format(DateTimeFormatter.ofPattern("yy-MM"));
    private static String baseDir = "D:\\dl\\" + date;

    public static void main(String[] args) {
        try {
            Path jsonPath = Paths.get("D:\\dl\\list.txt");
            Scanner scanner = new Scanner(jsonPath);
            int total = 0;
            String line = "";
            ExecutorService executor = Executors.newCachedThreadPool();
            // java21 虚拟线程
            // try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
                while (scanner.hasNextLine()) {
                    line = scanner.next();
                    System.out.println("处理：" + line);
                    if (line == null || line.trim().equals("")) {
                        break;
                    }
                    String imageUrl = line.split(",")[0];
                    String fileName = line.split(",")[1];
                    if (fileName == null || fileName.trim().equals("")) {
                        continue;
                    }
                    /* */
                    executor.submit(() -> {
                        downloadUsingNIO(imageUrl, baseDir + fileName);
                        return 1;
                    });
                    // new Thread(() -> downloadUsingNIO(imageUrl, baseDir + fileName)).start();
                    // Thread.startVirtualThread(() -> downloadUsingNIO(imageUrl, baseDir + fileName));
                }
            // }
            TimeUnit.SECONDS.sleep(100);
            // downloadUsingStream(url, "D:/users/maxsu/sitemap_stream.xml");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(1);
        }
    }

    private static void downloadUsingStream(String urlStr, String file) throws IOException {
        URL url = new URL(urlStr);
        BufferedInputStream bis = new BufferedInputStream(url.openStream());
        FileOutputStream fis = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int count = 0;
        while ((count = bis.read(buffer, 0, 1024)) != -1) {
            fis.write(buffer, 0, count);
        }
        fis.close();
        bis.close();
    }

    private static void downloadUsingNIO(String urlStr, String file) {
        System.out.println("开始：" + file);
        try {
            URL url = new URL(urlStr);
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            FileOutputStream fos = new FileOutputStream(file);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
            rbc.close();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            System.out.println("完成：" + file);
        }
    }

}


